package com.zr.plant.controller;

import com.zr.meiju.StatusEnum;
import com.zr.meiju.StatusValueEnum;
import com.zr.meiju.StatusZhongWenEnum;
import com.zr.plant.pojo.*;
import com.zr.plant.service.PlantService;
import com.zr.util.AllRecords;
import com.zr.util.ResultBuildVo;
import com.zr.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.xml.transform.Result;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 86151 on 2019/6/14.
 */
@RestController
@CrossOrigin
public class PlantController {
    @Autowired
    private PlantService plantService;

    /**
     * 新增接口
     * @param plantAddVo
     * @param bindingResult
     * @return
     */
//    @PostMapping("/legalPlant/add")
//    public ResultVo addPlant(@RequestBody @Valid PlantAddVo plantAddVo, BindingResult bindingResult){
//       if (bindingResult.hasErrors()){
//           return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage(),"500");
//       }
//        return plantService.addPlant(plantAddVo);
//    }
    @PostMapping("/legalPlant/add")
    public ResultVo addPlanClass(@RequestBody @Valid PlantAddVo plantAddVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage(),"500");
        }
        List<PlantAddVo> planClassAddVoList = new ArrayList<>();
        planClassAddVoList.add(plantAddVo);
        return plantService.addPlant(planClassAddVoList);
    }

    /**
     * 修改接口
     * @param plantUpdateVo
     * @param bindingResult
     * @return
     */
    @PostMapping("legalPlant/update")
    public ResultVo updatePlant(@RequestBody @Valid PlantUpdateVo plantUpdateVo, BindingResult bindingResult){
       if (bindingResult.hasErrors()){
           return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage(),"500");
       }
        return plantService.updatePlant(plantUpdateVo);
    }

    /**
     * 修改状态接口
     * @param plantStatusVo
     * @param bindingResult
     * @return
     */
    @PostMapping("legalPlant/updatestatus")
    public ResultVo updateStatus(@RequestBody @Valid PlantStatusVo plantStatusVo, BindingResult bindingResult){
       if (bindingResult.hasErrors()){
           return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage(),"500");
       }
        return plantService.updateStatus(plantStatusVo);
    }



    /**
     * 法人下拉框
     */
    @GetMapping("queryDownByUserId")
    public ResultVo<List<Legalperson>> queryDownByUserId(@RequestParam("uid")Integer id){
        return plantService.queryDown(id);
    }


    /**
     * 工厂查看接口
     */
    @GetMapping("queryPlantById")
    public ResultVo<PlantClass> queryPlantById(@RequestParam("id")Integer id){
        return plantService.queryPlantById(id);
    }

    /**
     * 分页查询接口
     */
    @PostMapping("legalPlant/query")
    public ResultVo<AllRecords> queryPage(@RequestBody PlantSelectVo plantSelectVo){

        return plantService.queryPage(plantSelectVo);
    }
    /**
     * 法人下拉框
     */
    @GetMapping("legalPerson/ofCurrentUser")
    public ResultVo<List<LegalPersonResultVo>> ofCurrentUser(){
        return plantService.ofCurrentUser();
    }

    /**
     * 导入
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("legalPlant/legalPlantImport")
    public ResultVo importPlanClass(MultipartFile file)throws Exception{
        return plantService.importPlanClass(file);
    }

    /**
     * 启用禁用下拉框
     * @return
     */
    @PostMapping("legalPlant/exportMater")
    @CrossOrigin
    public ResultVo exportMater(){
        return ResultBuildVo.success(null);
    }

    /**
     * 导出
     * @param resp
     * @param remark
     * @param code
     * @param status
     * @param legalPersonCode
     * @return
     * @throws Exception
     */
    @RequestMapping("material/exportMater")
    public ResultVo exportExcel(HttpServletResponse resp, String remark, String code, Boolean status,String legalPersonCode) throws Exception{
        PlantSelectVo plantSelectVo = new PlantSelectVo();
        plantSelectVo.setLegalPlantCode(remark);
        plantSelectVo.setLegalPlantDec(code);
        plantSelectVo.setEnabled(status);
        plantSelectVo.setLegalPersonCode(legalPersonCode);
        return plantService.exportExcel(resp,plantSelectVo);
    }


}
